<?php
namespace App\Http\Middleware;


use App\Exception\MaintenanceException;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Swoft\Bean\Annotation\Mapping\Bean;
use Swoft\Console\Router\Module;
use Swoft\Http\Message\Request;
use Swoft\Http\Message\Response;
use Swoft\Http\Server\Contract\MiddlewareInterface;

/**
 * 系统维护检测
 * Class MaintenanceMiddleware
 * @package App\Http\Middleware
 * @Bean("MaintenanceMiddleware")
 */
class MaintenanceMiddleware implements MiddlewareInterface
{

    /**
     * @inheritDoc
     */
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
    {
        // 判断 是否开启系统维护
        if (constant(Module::DEFINE_ENABLE_SYSTEM_MAINTAIN)) {
            // 开启了系统维护
            throw new MaintenanceException();
        }
        return $handler->handle($request);
    }
}
